Skip to content

fix: keep USING / WITH CHECK parentheses for scalar subquery policy expressions (#718) - #721

Merged
tianzhou merged 2 commits into
mainfrom
devin/1795756638-policy-subquery-parens
Nov 27, 2026
Merged

tianzhou merged 2 commits into
mainfrom
devin/1795756638-policy-subquery-parens

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #718. ensureParentheses (policy.go) treated any expression that starts with ( and ends with ) as already wrapped. pg_get_expr renders a scalar subquery as ( SELECT ... ), so USING ((SELECT is_allowed())) came out as USING ( SELECT is_allowed() AS is_allowed), which PostgreSQL rejects. This broke CREATE POLICY and ALTER POLICY in dump/plan/apply.

An expression now counts as wrapped only when:

  • its first ( is closed by its last ) (uses the existing matchingParen, which skips string literals and quoted identifiers, so (a) OR (b) is no longer treated as wrapped), and
  • the enclosed text does not start with the keyword SELECT, WITH or VALUES (keywordAt checks word boundaries, so (with_flag) is still treated as wrapped).
USING ( SELECT is_admin() AS is_admin)   ->  USING (( SELECT is_admin() AS is_admin))

Tests

  • create_policy/add_policy: added CREATE POLICY cases with a scalar subquery in USING and in WITH CHECK
  • create_policy/alter_policy_using: added an ALTER POLICY case where the USING scalar subquery changes
  • Added TestEnsureParentheses unit test

Link to Devin session: https://app.devin.ai/sessions/314326a464fb4b3c9fbe6ccc32736cdf
Open in Devin Desktop: https://app.devin.ai/desktop/session/314326a464fb4b3c9fbe6ccc32736cdf?variant=devin
Requested by: @tianzhou


🟢 Devin Review found nothing to fix · View findings

Devin Review

…xpressions (#718)

Co-Authored-By: Tianzhou <t@bytebase.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · This PR appears safe to merge; no actionable issues were found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge; no actionable issues were found.

Summary

Fixes policy SQL that mistakes a scalar subquery’s parentheses for the parentheses required by USING or WITH CHECK.

  • Policy SQL keeps scalar subqueries inside the clause parentheses.

Reviews (1) · Last reviewed commit: "fix: keep USING / WITH CHECK parentheses..." · Reviewed by Greptile

This comment was marked as resolved.

…licy clauses

Co-Authored-By: Tianzhou <t@bytebase.com>
@tianzhou
tianzhou merged commit 008e66e into main Nov 27, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CREATE/ALTER POLICY loses its USING / WITH CHECK parentheses when the expression is a scalar subquery

2 participants